package com.zakler.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zakler.po.Student;
import com.zakler.po.Teacher;
import com.zakler.util.HibernateUtil;

@Results({
	@Result(name="add_success",location="student_list",type="redirectAction"),
	@Result(name="list_success",location="/adds.jsp",type=""),
	@Result(name="searchall_success",location="/modify2.jsp",type=""),
	@Result(name="remove_success",location="student_list",type="redirectAction"),
	@Result(name="modify_success",location="student_list",type="redirectAction")	
})
public class StudentAction extends BaseAction
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Student student;
	private Integer tempid;
	
	// add method
	@Action("student_add")
	public String add() throws Exception
	{
		Session session = HibernateUtil.putSession();
		Transaction transaction = session.beginTransaction();
		session.save(student);
		transaction.commit();
		session.close();
		return "add_success";
	}
	
	// list method
	@Action("student_list")
	public String list() throws Exception
	{
		Session session = HibernateUtil.putSession();
		Query query = session.createQuery("from Student");
		@SuppressWarnings("unchecked")
		List<Teacher> list = query.list();
		session.close();
		getHttpServletRequest().getSession().setAttribute("Slist", list);
		return "list_success";
	}
	
	//search all method
	@Action("student_searchAll")
	public String searchAll() throws Exception
	{

		Session session = HibernateUtil.putSession();
		Query query = session.createQuery("from Student");
		@SuppressWarnings("unchecked")
		List<Teacher> list = query.list();
		session.close();
		getHttpServletRequest().getSession().setAttribute("Slist", list);
		getHttpServletRequest().getSession().setAttribute("SJNum", tempid);
		return "searchall_success";
	}
	
	//modify method
	@Action("student_modify")
	public String modify() throws Exception
	{

		Session session = HibernateUtil.putSession();
		session.update(student);
		Transaction transaction =session.beginTransaction();
		transaction.commit();
		session.close();
		return "modify_success";
	}
	// remove method
	@Action("student_remove")
		public String remove() throws Exception
		{
			Session session = HibernateUtil.putSession();
			Query query=session.createQuery("delete from Student where id="+tempid.toString());
			query.executeUpdate();
			Transaction transaction =session.beginTransaction();
			transaction.commit();
			session.close();
			return "remove_success";
		}
	
	public Student getStudent()
	{
		return student;
	}
	public void setStudent(Student student)
	{
		this.student = student;
	}

	public Integer getTempid()
	{
		return tempid;
	}

	public void setTempid(Integer tempid)
	{
		this.tempid = tempid;
	}
	
	
}
